package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.mapper.AdminOrderMapper;
import com.sky.result.PageResult;
import com.sky.service.AdminOrderService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;

import java.time.LocalDateTime;
import java.util.List;

import lombok.RequiredArgsConstructor;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;


/**
 * 订单接口实现类
 *
 * @author iTaotao
 * @version 1.0
 * @date 2023/08/04 16:30:25
 */
@Service
@RequiredArgsConstructor
public class AdminOrderServiceImpl implements AdminOrderService {
    private final AdminOrderMapper adminOrderMapper;

    @Override
    public void cancelOrder(OrdersCancelDTO ordersCancelDTO) {
        Orders orders = new Orders();
        BeanUtils.copyProperties(ordersCancelDTO, orders);
        orders.setCancelTime(LocalDateTime.now());
        adminOrderMapper.cancelOrder(orders);


    }

    @Override
    public PageResult selectPage(OrdersPageQueryDTO ordersPageQueryDTO) {
        PageHelper.startPage(ordersPageQueryDTO.getPage(), ordersPageQueryDTO.getPageSize());
        Orders orders = new Orders();
        BeanUtils.copyProperties(ordersPageQueryDTO , orders);

        Page<Orders> ordersPage = adminOrderMapper.selectPage(orders);
        return new PageResult(ordersPage.getTotal() , ordersPage.getResult());


    }

    @Override
    public OrderStatisticsVO OrdersStatis() {
        OrderStatisticsVO orderStatisticsVO = new OrderStatisticsVO();
        Integer ToBeConfirmed = adminOrderMapper.selectstatusByTwo();
        Integer Confirmed = adminOrderMapper.selectStatusByThree();
        Integer DeliveryInProgress = adminOrderMapper.selectstatusByFor();
        orderStatisticsVO.setToBeConfirmed(ToBeConfirmed);
        orderStatisticsVO.setConfirmed(Confirmed);
        orderStatisticsVO.setDeliveryInProgress(DeliveryInProgress);

        return orderStatisticsVO;
    }

    @Override
    public void confirmOrder(OrdersConfirmDTO ordersConfirmDTO) {
        Orders orders = new Orders();
        BeanUtils.copyProperties(ordersConfirmDTO , orders);
        adminOrderMapper.updateOrderStatus(orders);
    }

    @Override
    public void rejectionOrder(OrdersRejectionDTO ordersRejectionDTO) {
        Orders orders = new Orders();
        BeanUtils.copyProperties(ordersRejectionDTO , orders);
        adminOrderMapper.rejectionOrder(orders);
    }

    @Override
    public void DeliveryOrder(Long id) {
        adminOrderMapper.DeliveryOrder(id);

    }

    @Override
    public void completeOrder(Long id) {
        Orders orders = new Orders();
        orders.setId(id);
        orders.setDeliveryTime(LocalDateTime.now());

        adminOrderMapper.completeOrder(orders);
    }

    @Override
    public OrderVO selectOrderDetailById(Long id) {
        Orders orders = adminOrderMapper.selectOrderById(id);
        List<OrderDetail> orderDetailList = adminOrderMapper.selectOrderDetailById(id);
        OrderVO orderVO = new OrderVO();
        orderVO.setOrderDetailList(orderDetailList);
        BeanUtils.copyProperties(orders , orderVO);
        return orderVO;


    }
}
